<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\system;


use app\admin\model\SystemConfig;
use app\admin\model\SystemMenu;
use app\admin\model\SystemQuick;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;

/**
 * @ControllerAnnotation(title="快捷入口管理")
 * Class Quick
 * @package app\admin\controller\system
 */
class Quick extends AdminController
{

    use \app\admin\traits\Curd;

    protected $sort = [
        'sort' => 'desc',
        'id'   => 'desc',
    ];

    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new SystemQuick();
    }

    public function dropData(){
        $quicks = SystemQuick::where('status',1)->select();
        $arr = [];
        foreach ($quicks as $quick){
            $arr[] = [
                'title' => $quick->title,
                'icon' => $quick->icon,
                'href' => $quick->href,
                'status' => $quick->status,
                'remark' => $quick->remark,
                'sort' => $quick->sort,
            ];
        }
        $save1 = app()->getBasePath()."../public/adapter/quicks.json";
        file_put_contents($save1,json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
        //导出system_config表配置
        $config = SystemConfig::select();
        $arr = [];
        foreach($config as $k=>$v){
            $arr[$k] = [
                'name' => $v['name'],
                'group' => $v['group'],
                'value' => $v['value'],
                'remark' => $v['remark'],
                'sort' => $v['sort'],
            ];
        }
        $save1 = app()->getBasePath()."../public/adapter/config.json";
        file_put_contents($save1,json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));
        //生成菜单
        $m1 = SystemMenu::whereIn('pid',['99999999',0])->select();
        $arr = [];
        $field = ['id','pid','title','icon','href','remark','sort','status','params','target'];
        foreach($m1 as $k=>$v){
            foreach($field as $f){
                $arr['m1'][$k][$f] = $v[$f] ;
            }
        }
        $m2 = SystemMenu::whereIn('pid', array_column($m1->toArray(), 'id'))->select();
        foreach($m2 as $k=>$v){
            $sb = [];
            foreach($field as $f){
                $sb[$f] = $v[$f] ;
            }
            $arr['m2'][$v['pid']][] =$sb ;
        }
        $m3 = SystemMenu::whereIn('pid', array_column($m2->toArray(), 'id'))->select();
        foreach($m3 as $k=>$v){
            $sb = [];
            foreach($field as $f){
                $sb[$f] = $v[$f] ;
            }
            $arr['m3'][$v['pid']][] =$sb ;
        }
        $save1 = app()->getBasePath()."../public/adapter/menus.json";
        file_put_contents($save1,json_encode($arr,JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT));

    }

}